//
//  WitleViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/1/6.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "WitleViewController.h"
#import "CreateManagerViewController.h"
#import "Header.h"
#import "NSString+Extension.h"
#import "MBProgressHUD+PKX.h"

@interface WitleViewController ()<UITextViewDelegate>

@property(nonatomic,assign) NSInteger           keyBoardHeight;
@property(nonatomic,assign) NSInteger           textLines;
@property(nonatomic,assign) BOOL                keyBoardState;

@end

@implementation WitleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航视图
    [self initNavigation];
    
    // 初始化通知相关设置
    [self initNotification];
    
    // 页面初始化
    [self initView];
}

/* - - - - - -- - - - - - - - -页面相关设置 - - - - - - - - - -- -  */

- (void) initView {
    
    [self.view setBackgroundColor:Bg_Color];
    
    [self.view addSubview:self.contextTextView];
    
    [self.contextTextView resignFirstResponder];
}

/* - - - - - -- - - - - - - - -设置导航控制器样式 - - - - - - - - - -- -  */

- (void) initNavigation {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    // 设置导航控制器左右按钮
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(action_createItemClickEvent)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}

/* - - - - - -- - - - - - - - -通知相关设置 - - - - - - - - - -- -  */

- (void) initNotification {
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

/* - - - - - -- - - - - - - - -代理事件相关设置 - - - - - - - - - -- -  */

- (void)textViewDidChange:(UITextView *)textView {
    
    CGSize textSize = [textView.text textSizeWithFont:[UIFont systemFontOfSize:16] maxSize:CGSizeMake(SCREEN_W, SCREEN_H)];
    
    if (textSize.height > SCREEN_H - 64 - _keyBoardHeight) {
        
        [self.view setFrame:CGRectMake(0, -_keyBoardHeight, SCREEN_W, SCREEN_H)];
    }
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    // 更新键盘状态
    _keyBoardState = YES;
    
    // 获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    
    // 获取键盘高度
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _keyBoardHeight = keyboardRect.size.height;
    
    CGSize textSize = [self.contextTextView.text textSizeWithFont:[UIFont systemFontOfSize:16] maxSize:CGSizeMake(SCREEN_W, SCREEN_H)];
    
    if (textSize.height > SCREEN_H - 64 - _keyBoardHeight) {
    
        [self.view setFrame:CGRectMake(0, -_keyBoardHeight, SCREEN_W, SCREEN_H)];
    }
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    // 更新键盘状态
    _keyBoardState = NO;
    
    // 设置视图的高度
    _keyBoardHeight = 0;
    
    [self.view setFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    
    [self.contextTextView setContentSize:CGSizeMake(SCREEN_W, SCREEN_H)];
}

/* - - - - - -- - - - - - - - -点击事件相关设置 - - - - - - - - - -- -  */

- (void) action_backItmeClickEvent {

    [self.contextTextView resignFirstResponder];
    [[CreateManagerViewController shardManger] showWitleView];
}

- (void) action_createItemClickEvent {
    
    [self.contextTextView resignFirstResponder];
    
    // 根据键盘状态，使用通知对数据进行传递
    if (self.contextTextView.text.length > 20) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"contextString" object:self.contextTextView.text];
        
        [[CreateManagerViewController shardManger] showWitleView];
    }else {
    
        [MBProgressHUD showError:@"文字的内容不能少于20字"];
    }
}

#pragma mark -- 懒加载
- (UITextView *)contextTextView {
    
    if (!_contextTextView) {
        
        _contextTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        [_contextTextView setTextColor:[UIColor grayColor]];
        
        [_contextTextView setDelegate:self];
        
        [_contextTextView setBackgroundColor:Btn_Color];
        
        [_contextTextView setFont:[UIFont systemFontOfSize:16]];
        
    }
    return _contextTextView;
}


@end
